package middleware

import (
	"context"
	"time"

	"github.com/devops-smartbot/devops-smartbot/shared/logger"
	"go.uber.org/zap"
	"google.golang.org/grpc"
)

// GRPCUnaryLogging provides basic request/response logging for unary gRPC handlers.
func GRPCUnaryLogging() grpc.UnaryServerInterceptor {
	return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
		start := time.Now()
		resp, err := handler(ctx, req)

		fields := []zap.Field{
			zap.String("method", info.FullMethod),
			zap.Duration("duration", time.Since(start)),
		}

		if err != nil {
			logger.L().Error("grpc request failed", append(fields, zap.Error(err))...)
		} else {
			logger.L().Info("grpc request completed", fields...)
		}

		return resp, err
	}
}
